<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.enable">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="执行程序">
        <el-input
          v-model="form.jsContent"
          :rows="9"
          placeholder="function main(argMap){}"
          type="textarea"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { save, get } from "@/api/parser";
import { Message } from "element-ui";
export default {
  data() {
    return {
      form: {
        _id: 0,
        name: "",
        eventType: "ON_SESSION_RECEIVED",
        enable: 1,
        jsContent: ""
      }
    };
  },
  created() {
    const id = this.$route.params && this.$route.params.id;
    if (id !== 0) {
      this.load(id);
    }
  },
  methods: {
    onSubmit() {
      save(this.form)
        .then(() => {
          this.$router.push({ path: "/parser/index" });
        })
        .catch(() => {
          Message({
            message: "Error",
            type: "error",
            duration: 5 * 1000
          });
        });
    },
    load(id) {
      get(id).then(response => {
        if (response.data.length > 0) {
          this.form = response.data[0];
        }
      });
    }
  }
};
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

